﻿<#
Name: Battery Check Script
Purpose: Silently run HP Battery Check and save results in Access file on the Server
Version: 1.1 - May, 2013
 
This script is provided "AS IS" with no warranties, confers no rights and
is not supported by the authors or deployment4everyone.
 
Author - Milos Pec
Blog : http://deployment4everyone.blogspot.com
Code Snipets from: http://technet.microsoft.com/en-us/magazine/2009.05.scriptingguys.aspx
#>
##########################################################
#Change this to your Access DB file on the server
$Db = "\\yourserver\deploy\batterycheck\BatteryCheck.mdb"
##########################################################

Function Check-Path($fl)
{
 If(!(Test-Path -path (Split-Path -path $fl -parent)))
   { 
     Throw "$(Split-Path -path $fl -parent) Does not Exist"
     exit
   }
  ELSE
  { 
   If(!(Test-Path -Path $fl))
     {
      Throw "$fl does not exist"
      exit
     }
  }
} #End Check-Path

Function Connect-Database($fl, $Tables)
{
  $OpenStatic = 3
  $LockOptimistic = 3
  $connection = New-Object -ComObject ADODB.Connection
  $connection.Open("Provider = Microsoft.Jet.OLEDB.4.0;Data Source=$Db" )
  Update-Records($Tables)
} #End Connect-DataBase

Function Update-Records($Tables)
{
  $RecordSet = new-object -ComObject ADODB.Recordset
   ForEach($Table in $Tables)
     {
      $Query = "Select * from $Table"
      $RecordSet.Open($Query, $Connection, $OpenStatic, $LockOptimistic)
      Invoke-Expression "Update-$Table"
      $RecordSet.Close()
     }
   $connection.Close()
} #End Update-Records

Function Update-BatteryInfo
{
    "Updating Battery Information"
    $RecordSet.AddNew()
    $RecordSet.Fields.Item("ComputerName") = $comp
    $RecordSet.Fields.Item("Model") = $model
    $RecordSet.Fields.Item("ComputerSerial") = $comp_serial
    $RecordSet.Fields.Item("BatterySerialNumber") = $batt_serial
    $RecordSet.Fields.Item("FailureID") = $failID
    $RecordSet.Fields.Item("ResultCode") = $result_code
    $RecordSet.Fields.Item("DesignCharge") = $design_charge
    $RecordSet.Fields.Item("MaxCharge") = $max_charge
    $RecordSet.Fields.Item("ChargeCapacity") = $charge_capacity
    $RecordSet.Fields.Item("DateRun") = $run_date
    $RecordSet.Update()
} #End Update-BatteryInfo


# Checking If Log File Exists
if (Test-Path "$env:ProgramData\Hewlett-Packard\HP Support Framework\Resources\Logs\BCResOut.xml") {
    [xml]$battery = get-content $env:ProgramData"Hewlett-Packard\HP Support Framework\Resources\Logs\BCResOut.xml"
} else {
   Throw "File not found ..." 
}

#Execute Battery Scan
Check-Path -fl "${env:ProgramFiles(x86)}\Hewlett-Packard\HP Support Framework\Resources\hpbc.exe"
Start-Process "${env:ProgramFiles(x86)}\Hewlett-Packard\HP Support Framework\Resources\hpbc.exe /S" -Wait

# Script Start
$comp = $env:COMPUTERNAME
$model = $battery.BatteryTest.ComputerProductName
$comp_serial = $battery.BatteryTest.ComputerSerialNumber
$design_charge = $battery.BatteryTest.result[0].p[0].InnerText
$max_charge = $battery.BatteryTest.result[0].p[1].InnerText
$batt_serial = $battery.BatteryTest.result[0].p[14].InnerText
$failID = $battery.BatteryTest.result[0].FailureID
$charge_capacity = $battery.BatteryTest.result[0].v[9].InnerText
$run_date = $battery.BatteryTest.result[0].r[0].InnerText
$result_code = $battery.BatteryTest.result[0].ResultCode

# If uncommented following will create CVS file on your desktop - to test results
#Start Creating New file
#$batt_status = $Env:USERPROFILE + "\Desktop\" + $failID + "." + $env:COMPUTERNAME + "." +(Get-Date -uFormat "%Y%m%d-%H%M") + ".csv"
#"ComputerName,Model,ComputerSerialNumber,BatterySerialNumber,FailureID,ResultCode,DesignCharge,MaxCharge,Charge_Capacity,Date" | Out-File -FilePath $batt_status -Encoding Ascii
#Add-Content -Encoding Ascii $batt_status ($comp + "," + $model + "," + $comp_serial + "," + $batt_serial + "," + $failID + "," + $result_code + "," + $design_charge + "," + $max_charge + "," + $charge_capacity + "," + $run_date).ToString()
#End Creating New file

if($env:PROCESSOR_ARCHITECTURE -eq 'x86')
  {
    $Tables = "BatteryInfo"
    Check-Path -fl $Db
    Connect-DataBase -fl $Db -tables $Tables
   }
#End Updating Access DB
